package br.com.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class LoginInterceptor extends HandlerInterceptorAdapter {
	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
				
		HttpSession session = request.getSession();
		
		String uri = request.getRequestURI();
		//System.out.println(uri.endsWith("login") + " " + uri + " "+ uri.endsWith("doLogin"));
        if(uri.endsWith("login")
           || uri.endsWith("doLogin")
           || uri.contains("bootstrap")){
	        
        	return true;
	    }
		
		//User is log
		if(session.getAttribute("loguser") != null)
			return true;
		
		if(session.getAttribute("superuser") != null
		   && (uri.contains("add") || uri.contains("lookingForZipcode"))) 
			return true;
			
		
		//User not is log
		//System.out.println("Passou aki");
		response.sendRedirect("login");
		return false;
	}
}
